

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>kospeech.data.audio.augment &mdash; KoSpeech 0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> KoSpeech
          

          
          </a>

          
            
            
              <div class="version">
                0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">NOTES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/intro.html">Intro</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/Preparation.html">Preparation before Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/opts.html">Options</a></li>
</ul>
<p class="caption"><span class="caption-text">ARCHITECTURE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Seq2seq.html">Seq2seq</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Transformer.html">Transformer</a></li>
</ul>
<p class="caption"><span class="caption-text">PACKAGE REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Checkpoint.html">Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Data.html">Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Decode.html">Decode</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Evaluator.html">Evaluator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Trainer.html">Trainer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Etc.html">Etc</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">KoSpeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>kospeech.data.audio.augment</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for kospeech.data.audio.augment</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">torch</span> <span class="k">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">kospeech.utils</span> <span class="k">import</span> <span class="n">logger</span>
<span class="kn">from</span> <span class="nn">kospeech.data.audio.core</span> <span class="k">import</span> <span class="n">split</span>


<div class="viewcode-block" id="SpecAugment"><a class="viewcode-back" href="../../../../Data.html#kospeech.data.audio.augment.SpecAugment">[docs]</a><span class="k">class</span> <span class="nc">SpecAugment</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provides Spec Augment. A simple data augmentation method for speech recognition.</span>
<span class="sd">    This concept proposed in https://arxiv.org/abs/1904.08779</span>

<span class="sd">    Args:</span>
<span class="sd">        time_mask_para (int): maximum time masking length</span>
<span class="sd">        freq_mask_para (int): maximum frequency masking length</span>
<span class="sd">        time_mask_num (int): how many times to apply time masking</span>
<span class="sd">        freq_mask_num (int): how many times to apply frequency masking</span>

<span class="sd">    Inputs: spectrogram</span>
<span class="sd">        - **spectrogram** (torch.FloatTensor): spectrogram feature from audio file.</span>

<span class="sd">    Returns: spectrogram:</span>
<span class="sd">        - **spectrogram**: masked spectrogram feature.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time_mask_para</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">freq_mask_para</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time_mask_num</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">freq_mask_num</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">time_mask_para</span> <span class="o">=</span> <span class="n">time_mask_para</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">freq_mask_para</span> <span class="o">=</span> <span class="n">freq_mask_para</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">time_mask_num</span> <span class="o">=</span> <span class="n">time_mask_num</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">freq_mask_num</span> <span class="o">=</span> <span class="n">freq_mask_num</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feature_vector</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot; Provides SpecAugmentation for audio &quot;&quot;&quot;</span>
        <span class="n">time_axis_length</span> <span class="o">=</span> <span class="n">feature_vector</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">freq_axis_length</span> <span class="o">=</span> <span class="n">feature_vector</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

        <span class="c1"># time mask</span>
        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time_mask_num</span><span class="p">):</span>
            <span class="n">t</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">time_mask_para</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">time_axis_length</span> <span class="o">-</span> <span class="n">t</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">t0</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">time_axis_length</span> <span class="o">-</span> <span class="n">t</span><span class="p">)</span>
                <span class="n">feature_vector</span><span class="p">[</span><span class="n">t0</span><span class="p">:</span> <span class="n">t0</span> <span class="o">+</span> <span class="n">t</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c1"># freq mask</span>
        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">freq_mask_num</span><span class="p">):</span>
            <span class="n">f</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">freq_mask_para</span><span class="p">))</span>
            <span class="n">f0</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">freq_axis_length</span> <span class="o">-</span> <span class="n">f</span><span class="p">)</span>
            <span class="n">feature_vector</span><span class="p">[:,</span> <span class="n">f0</span><span class="p">:</span> <span class="n">f0</span> <span class="o">+</span> <span class="n">f</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">return</span> <span class="n">feature_vector</span></div>


<div class="viewcode-block" id="NoiseInjector"><a class="viewcode-back" href="../../../../Data.html#kospeech.data.audio.augment.NoiseInjector">[docs]</a><span class="k">class</span> <span class="nc">NoiseInjector</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provides noise injection for noise augmentation.</span>
<span class="sd">    The noise augmentation process is as follows:</span>

<span class="sd">    Step 1: Randomly sample audios by `noise_size` from dataset</span>
<span class="sd">    Step 2: Extract noise from `audio_paths`</span>
<span class="sd">    Step 3: Add noise to sound</span>

<span class="sd">    Args:</span>
<span class="sd">        dataset_path (str): path of dataset</span>
<span class="sd">        noiseset_size (int): size of noise dataset</span>
<span class="sd">        sample_rate (int): sampling rate</span>
<span class="sd">        noise_level (float): level of noise</span>

<span class="sd">    Inputs: signal</span>
<span class="sd">        - **signal**: signal from pcm file</span>

<span class="sd">    Returns: signal</span>
<span class="sd">        - **signal**: noise added signal</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset_path</span><span class="p">,</span> <span class="n">noiseset_size</span><span class="p">,</span> <span class="n">sample_rate</span><span class="o">=</span><span class="mi">16000</span><span class="p">,</span> <span class="n">noise_level</span><span class="o">=</span><span class="mf">0.7</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">):</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Directory doesn`t exist: </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">))</span>
            <span class="k">raise</span> <span class="ne">IOError</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Create Noise injector...&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">noiseset_size</span> <span class="o">=</span> <span class="n">noiseset_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sample_rate</span> <span class="o">=</span> <span class="n">sample_rate</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">noise_level</span> <span class="o">=</span> <span class="n">noise_level</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">audio_paths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_audio_paths</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dataset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_noiseset</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">)</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Create Noise injector complete !!&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">signal</span><span class="p">):</span>
        <span class="n">noise</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset</span><span class="p">)</span>
        <span class="n">noise_level</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">noise_level</span><span class="p">)</span>

        <span class="n">signal_length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>
        <span class="n">noise_length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">noise</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">signal_length</span> <span class="o">&gt;=</span> <span class="n">noise_length</span><span class="p">:</span>
            <span class="n">noise_start</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="n">signal_length</span> <span class="o">-</span> <span class="n">noise_length</span><span class="p">))</span>
            <span class="n">noise_end</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">noise_start</span> <span class="o">+</span> <span class="n">noise_length</span><span class="p">)</span>
            <span class="n">signal</span><span class="p">[</span><span class="n">noise_start</span><span class="p">:</span> <span class="n">noise_end</span><span class="p">]</span> <span class="o">+=</span> <span class="n">noise</span> <span class="o">*</span> <span class="n">noise_level</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">signal</span> <span class="o">+=</span> <span class="n">noise</span><span class="p">[:</span><span class="n">signal_length</span><span class="p">]</span> <span class="o">*</span> <span class="n">noise_level</span>

        <span class="k">return</span> <span class="n">signal</span>

    <span class="k">def</span> <span class="nf">create_audio_paths</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset_path</span><span class="p">):</span>
        <span class="n">audio_paths</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">data_list</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">)</span>
        <span class="n">data_list_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_list</span><span class="p">)</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">index</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">*</span> <span class="n">data_list_size</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">data_list</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.pcm&#39;</span><span class="p">):</span>
                <span class="n">audio_paths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data_list</span><span class="p">[</span><span class="n">index</span><span class="p">])</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">audio_paths</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">noiseset_size</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">audio_paths</span>

    <span class="k">def</span> <span class="nf">create_noiseset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset_path</span><span class="p">):</span>
        <span class="n">dataset</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">audio_path</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">audio_paths</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dataset_path</span><span class="p">,</span> <span class="n">audio_path</span><span class="p">)</span>
            <span class="n">noise</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extract_noise</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">noise</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">dataset</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">noise</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">dataset</span>

    <span class="k">def</span> <span class="nf">extract_noise</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">audio_path</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">signal</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">memmap</span><span class="p">(</span><span class="n">audio_path</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">)</span>
            <span class="n">non_silence_indices</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">top_db</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>

            <span class="k">for</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span> <span class="ow">in</span> <span class="n">non_silence_indices</span><span class="p">:</span>
                <span class="n">signal</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="n">noise</span> <span class="o">=</span> <span class="n">signal</span><span class="p">[</span><span class="n">signal</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">noise</span> <span class="o">/</span> <span class="mi">32767</span>

        <span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;RuntimeError in </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">audio_path</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;RuntimeError in </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">audio_path</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">None</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Soohwan Kim

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>